--[[

	GModRP - Real Garry's Mod Roleplay.
			By Slob187.
		

]]


GMRP.Items = {}

function GMRP.AddItem( itemtable )

	GMRP.Items[itemtable.ID] =
	{
		Name = itemtable.Name,	
		Price = itemtable.Price,
		Info = itemtable.Info,	
		Type = itemtable.Type,
		Energy = itemtable.Energy,
		Model = itemtable.Model,
		Spawn = itemtable.Spawn,
		Use = itemtable.Use,
		Remove = itemtable.Remove,
	}
	
end	


if (!SERVER) then return end

function GMRP.BaseItemSpawn( p, itemtable )

	local trace = {}
	trace.start = p:EyePos()
	trace.endpos = trace.start + p:GetAimVector() * 85
	trace.filter = p
	local tr = util.TraceLine(trace)	


	local sitem = ents.Create( "prop_physics" )
	sitem:SetModel( itemtable.Model )
	sitem:SetPos( tr.HitPos )
	sitem:Spawn()
	sitem:Activate()
	sitem:GetPhysicsObject():Wake()	
	sitem:EmitSound( "physics/cardboard/cardboard_box_break3.wav" )
	sitem.ID = itemtable.ID
	sitem.IsItem = true
	sitem.Use = itemtable.Use
	sitem.Removeable = itemtable.Remove
	
end


function GMRP.BaseUse( p, itemtable )	
	
	p:AddEnergy( itemtable.Energy )

end


function GMRP.BaseVehicle( p, mdl, ent, script, itemtable )

	local trace = {}
	trace.start = p:EyePos()
	trace.endpos = trace.start + p:GetAimVector() * 130
	trace.filter = p
	local tr = util.TraceLine( trace )	

	local jeep = ents.Create( ent )
	jeep:SetModel( mdl )	
	jeep:SetKeyValue( "actionScale", 1 ) 
 	jeep:SetKeyValue( "VehicleLocked", 0 ) 
 	jeep:SetKeyValue( "solid", 6 ) 
	jeep:SetKeyValue( "vehiclescript", script ) 		
	jeep:SetPos( tr.HitPos )
	jeep:Spawn()
	jeep:Activate()	
	jeep.ID = itemtable.ID
	jeep.Use = itemtable.Use	
	
end	


function GMRP.BaseBook( p, itemtable )

	local trace = {}
	trace.start = p:EyePos()
	trace.endpos = trace.start + p:GetAimVector() * 85
	trace.filter = p
	local tr = util.TraceLine(trace)	

	local sbook = ents.Create( "prop_physics" )
	sbook:SetModel( itemtable.Model )
	sbook:SetPos( tr.HitPos )
	sbook:Spawn()
	sbook:Activate()
	sbook:GetPhysicsObject():Wake()	
	sbook:EmitSound( "physics/cardboard/cardboard_box_break3.wav" )
	sbook.ID = itemtable.ID
	sbook.IsItem = true
	sbook.Use = itemtable.Use
	
end


function GMRP.BaseBookUse( p, ent )

	if ent.ID != nil then
	
		if GMRP.UseLimit( p ) then --in case if player will use book too many times
	
			p:SendLua( "GMRP.BookMenu( '"..ent.ID.."' )" )
			
		end	

	end

end	

//I made the stupidest Lua module ever: http://www.facepunch.com/showthread.php?t=770585
function GMRP.BaseSent( p, class, itemtable )

	local trace = {}
	trace.start = p:EyePos()
	trace.endpos = trace.start + p:GetAimVector() * 85
	trace.filter = p
	local tr = util.TraceLine(trace)	

	local sent = ents.Create( class )
	sent:SetPos( tr.HitPos )
	sent:Spawn()
	sent:Activate()
	sent:GetPhysicsObject():Wake()	
	sent:EmitSound( "physics/cardboard/cardboard_box_break3.wav" )
	sent.ID = itemtable.ID
	sent.IsItem = true
	sent.Use = itemtable.Use
	
end
	

function GMRP.BaseCreator( p, pt, itemtable )

	local trace = {}
	trace.start = p:EyePos()
	trace.endpos = trace.start + p:GetAimVector() * 85
	trace.filter = p
	local tr = util.TraceLine(trace)
	
	local creator = ents.Create( "prop_physics" )
	creator:SetModel( itemtable.Model )
	creator:SetPos( tr.HitPos )
	creator:Spawn()
	creator:Activate()
	creator:SetMoveType( MOVETYPE_NONE )
	creator:EmitSound( "physics/cardboard/cardboard_box_break3.wav" )
	creator.ID = itemtable.ID
	creator.IsItem = true
	local cEntIndex = creator:EntIndex()
	

	creator.Use = function( p, ent )

		if ( p.plantsTime == nil ) then
		
			p.plantsTime = {}
			
		end	
	
		if ( p.plantsTime[cEntIndex] == nil ) then
		
			p.plantsTime[cEntIndex] = 0
			
		end
		
		if ( CurTime() > p.plantsTime[cEntIndex] ) then
	
			local itemtable2 = GMRP.Items[pt]
			
			local created = ents.Create( "prop_physics" )
			created:SetModel( itemtable2.Model )
			created:SetPos( creator:GetPos() + Vector( 0, 0, 100 ) )
			created:Spawn()
			created:Activate()
			created:GetPhysicsObject():Wake()	
			created:EmitSound( "physics/cardboard/cardboard_box_break3.wav" )
			created.ID = pt
			created.IsItem = true
			created.Use = itemtable2.Use
			created.Removeable = itemtable2.Remove

			p.plantsTime[cEntIndex] = CurTime() + 60
			
		else

			p:RedNotify( "This plant does not have fruits yet, please try again in "..math.Round( p.plantsTime[cEntIndex] - CurTime() ).." seconds." )

		end	

	end
	
end	